<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;

class LiveCreatePost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required|max:50',
            'live_img' => 'required|max:255',
            'is_record' => 'required',
            'start_time' => 'required',
            'end_time' => 'required',
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        if ($this->wantsJson() || $this->ajax()) {
            throw new HttpResponseException(
                new JsonResponse([
                    'code' => 400,
                    'msg' => $validator->errors()->first(),
                    'data' => new \stdClass()
                ])
            );
        } else {
            parent::failedValidation($validator);
        }
    }
}
